# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import MySQLdb
import time


class DoubanPipeline(object):
    def process_item(self, item, spider):
        print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
        print("link ===============" + item['link'])
        return item
class MysqlPipeline(object):
    def __init__(self):
        #连接数据库
        self.connect = MySQLdb.Connect(host = '118.25.42.128',port = 3306,user = 'root',passwd = 'Scysuper12#$',db = 'douban',charset = 'utf8')
        self.cursor = self.connect.cursor()

    def process_item(self, item, spider):                     
        try:
            print("test douban insert into mysql")
            self.cursor.execute(
                    """insert into book(link,name,create_date) value(%s, %s, str_to_date(%s,'%%Y-%%m-%%d %%H:%%i:%%s'))""",
                    (item['link'],item['name'], time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
                                )
            self.connect.commit()

        except Exception as error:
            print(error)
    
        return item
